package in.antany.eclipse.file.handler;

import in.antany.eclipse.file.handler.helper.BuildPathUtils;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.core.commands.IHandlerListener;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.handlers.HandlerUtil;

public class CopyToBuildPath implements IHandler {

	@Override
	public void addHandlerListener(IHandlerListener handlerListener) {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		// TODO Auto-generated method stub

		String fileName = null;
		String srcFolderPath = null;
		IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
		if (editorPart != null) {
			IEditorInput iEditorInput = editorPart.getEditorInput();
			if (iEditorInput != null) {
				IFileEditorInput ife = (IFileEditorInput) iEditorInput;
				if (ife != null) {
					fileName = ife.getName();
					String path = ife.getFile().getLocation().toOSString();
					srcFolderPath = path
							.substring(
									0,
									(path.lastIndexOf("/") == -1 ? path
											.lastIndexOf("\\") : path
											.lastIndexOf("/")) + 1);
				}
			}

		}
		if (fileName != null && srcFolderPath != null) {
			BuildPathUtils.copyFilesIntoBuildPath(fileName, srcFolderPath);
		} else {
			MessageDialog.openWarning(HandlerUtil.getActiveShell(event),
					"Ant's file utils",
					"Incorrect File or Folder Path or No file exists");
		}
		return null;
	}

	@Override
	public boolean isEnabled() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isHandled() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void removeHandlerListener(IHandlerListener handlerListener) {
		// TODO Auto-generated method stub

	}

}
